package com.dminvoicemanager.itemmaster;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.dminvoicemanager.R;

public class DM_Items_Stocks_Information extends Activity {
	private static final String TAG = null;
	private SQLiteDatabase mydb;
	private String DBNAME = "dminvoicemanager.db";
	String CurrentRefItem, strMainQOH, strVanQOH, strQOH, strItemName;

	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(), DM_Item_Master.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_items_stockout_information);

		TextView txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Stock In Hand");
		selectCurrentItemCode();
	}

	private void selectCurrentItemCode() {
		// TODO Auto-generated method stub
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCurrentItems = null;
		try {
			String query = "SELECT * from FItemsMasterSum";
			curCurrentItems = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCurrentItems != null) {
			if (curCurrentItems.getCount() > 0) {
				while (curCurrentItems.moveToNext()) {
					CurrentRefItem = curCurrentItems.getString(1);
					Log.v(TAG, "CurrentRefItem XXX :" + CurrentRefItem);
					filterItemsOutstanding();
				}
			}
		}
		curCurrentItems.close();
		mydb.close();
	}

	private void filterItemsOutstanding() {
		// TODO Auto-generated method stub
		ListView lvtypedes = (ListView) findViewById(R.id.dmitemsoustandrevisionlist);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "colitempri_1", "colitempri_2",
				"colitempri_3" };
		int[] totrdes = new int[] { R.id.item_out_name, R.id.item_out_mainqty,
				R.id.item_out_vanqty };
		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();

		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor chkItemdOutstand = null;
		try {
			String queryChkItemInfo = "SELECT FI.ItemCode,FI.ItemName,ILD.QOH,ILD.LocCode FROM fItems FI,fItemLoc ILD  Where ILD.ItemCode =  FI.ItemCode AND FI.ItemCode = '"
					+ CurrentRefItem + "'";
			chkItemdOutstand = mydb.rawQuery(queryChkItemInfo, null);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (chkItemdOutstand != null) {
			if (chkItemdOutstand.getCount() > 0) {
				while (chkItemdOutstand.moveToNext()) {
					String strLocCode = chkItemdOutstand.getString(3);
					strItemName = chkItemdOutstand.getString(1);
					strQOH = chkItemdOutstand.getString(2);
					Log.v(TAG, "strQOH :" + strLocCode + "/" + strQOH);
					if (strLocCode.equals("MS01")) {
						strMainQOH = strQOH;
					} else if (strLocCode.equals("LS01")) {
						strVanQOH = strQOH;
					}

				}
				HashMap<String, String> maptrdes = new HashMap<String, String>();
				maptrdes.put("colitempri_1", strItemName);
				maptrdes.put("colitempri_2", strMainQOH);
				maptrdes.put("colitempri_3", strVanQOH);

				fillMapstrdes.add(maptrdes);
				// fill in the grid_item layout
				SimpleAdapter adaptertrdes = new SimpleAdapter(this,
						fillMapstrdes,
						R.layout.dm_items_stockout_information_raw, fromtrdes,
						totrdes);
				lvtypedes.setAdapter(adaptertrdes);
			}
		}
		chkItemdOutstand.close();
		mydb.close();
	}

	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
